<?php
$articleService = new ArticleService();
$agingService = new AgingServices();
$tool = new Tool();
$smarty->assign('tool', $tool);
if(isset($_GET['opt']))
{
	$opt = $_GET['opt'];
	$moduleID = $moduleService->getModuleIDByFuncAndOpt($context, $result, $func, $opt);
	$permit = $permitService->checkPermition($context, $result, $_SESSION['user']['type'], $moduleID);

	if($opt == "add" && $permit ==true)
	{
		$colourList = $colourService->findAllColour($context, $result);
		$lineList = $lineService->findAllLine($context, $result);
		$woodList = $woodService->findAllWood($context, $result);
		$agingList = $agingService->findAllAging($context, $result);
		$smarty->assign('agingList', $agingList);
		$smarty->assign('colourList', $colourList);
		$smarty->assign('lineList', $lineList);
		$smarty->assign("woodList",$woodList);
		$smarty->display($templateDir . "/temp1/addArticle.tpl");
	}

	if($opt == "edit" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$article = $articleService->getArticleByOid($context, $result, $_GET['id']);
			$colourList = $colourService->findAllColour($context, $result);
			$lineList = $lineService->findAllLine($context, $result);
			$woodList = $woodService->findAllWood($context, $result);
			$agingList = $agingService->findAllAging($context, $result);
			$smarty->assign('agingList', $agingList);
			$smarty->assign('article', $article);
			$smarty->assign('colourList', $colourList);
			$smarty->assign('lineList', $lineList);
			$smarty->assign("woodList",$woodList);
			$data = array();
			$data['imagePath0'] = $article->getArticleDomain()->getImagePath0();
			$data['imagePath1'] = $article->getArticleDomain()->getImagePath1();
			$data['imagePath2'] = $article->getArticleDomain()->getImagePath2();
			$data['imagePath3'] = $article->getArticleDomain()->getImagePath3();
			$smarty->assign('tool', $tool);
			$smarty->assign('data', $data);
			$smarty->display($templateDir . "/temp1/editArticle.tpl");

		}
	}
	if($opt == "list" && $permit ==true)
	{
		$start = $p->findStart($configPage['limit']);
		$count = $articleService->countAllArticle($context, $result);
		$pages = $p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$articleArr = $articleService->getAllArticleByRange($context, $result, $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$colourList = $colourService->findAllColour($context, $result);
		$lineList = $lineService->findAllLine($context, $result);
		$woodList = $woodService->findAllWood($context, $result);
		$smarty->assign('colourList', $colourList);
		$smarty->assign('lineList', $lineList);
		$smarty->assign("woodList",$woodList);
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);

		$smarty->assign('articleArr', $articleArr);
			
		$data = array();
		foreach ($articleArr as $article)
		{
			$data['imagePath0'] = $article->getArticleDomain()->getImagePath0();
			$data['imagePath1'] = $article->getArticleDomain()->getImagePath1();
			$data['imagePath2'] = $article->getArticleDomain()->getImagePath2();
			$data['imagePath3'] = $article->getArticleDomain()->getImagePath3();
		}
		$smarty->assign('data', $data);
			
		$smarty->display($templateDir . "/temp1/listArticle.tpl");
			
	}
	if($opt == "addColor" && $permit ==true)
	{
		$smarty->display($templateDir . "/temp1/addColor.tpl");
	}
	if($opt == "colorList" && $permit ==true)
	{
		$start = $p->findStart($configPage['limit']);
		$count = $colourService->coundAllColour($context, $result);
		$pages = $p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$colorArr = $colourService->findAllByRange($context, $result, $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
			
		$smarty->assign('colorArr', $colorArr);
		$smarty->display($templateDir . "/temp1/listColor.tpl");
	}

	if($opt == "editColor" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$color = $colourService->findAllByOid($context, $result, $_GET['id']);
			$smarty->assign('color', $color);
			$smarty->display($templateDir . "/temp1/editColor.tpl");

		}
	}
	if($opt == "addAging" && $permit ==true)
	{
		$smarty->display($templateDir . "/temp1/addAging.tpl");
	}
	if($opt == "agingList" && $permit ==true)
	{
		$start = $p->findStart($configPage['limit']);
		$count = $agingService->coundAllAging($context, $result);
		$pages = $p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$agingArr = $agingService->findAllByRange($context, $result, $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
			
		$smarty->assign('agingArr', $agingArr);
		$smarty->display($templateDir . "/temp1/listAging.tpl");
	}

	if($opt == "editAging" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$aging = $agingService->findAllByOid($context, $result, $_GET['id']);
			$smarty->assign('aging', $aging);
			$smarty->display($templateDir . "/temp1/editAging.tpl");

		}
	}

	if($opt == "addLine" && $permit ==true)
	{
		$smarty->display($templateDir . "/temp1/addLine.tpl");
	}

	if($opt == "lineList" && $permit ==true)
	{
		$start = $p->findStart($configPage['limit']);
		$count = $lineService->coundAllLine($context, $result);
		$pages = $p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$lineArr = $lineService->findAllByRange($context, $result, $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);

		$smarty->assign('lineArr', $lineArr);
		$smarty->display($templateDir . "/temp1/listLine.tpl");
	}

	if($opt == "editLine" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$line = $lineService->findAllByOid($context, $result, $_GET['id']);
			$smarty->assign('line', $line);
			$smarty->display($templateDir . "/temp1/editLine.tpl");

		}
	}

	if($opt == "addWood" && $permit ==true)
	{
		$smarty->display($templateDir . "/temp1/addWood.tpl");
	}

	if($opt == "woodList" && $permit ==true)
	{
		$start = $p->findStart($configPage['limit']);
		$count = $woodService->coundAllWood($context, $result);
		$pages = $p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$woodArr = $woodService->findAllByRange($context, $result, $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);

		$smarty->assign('woodArr', $woodArr);
		$smarty->display($templateDir . "/temp1/listWood.tpl");
	}

	if($opt == "editWood" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$wood = $woodService->findWoodByOid($context, $result, $_GET['id']);
			$smarty->assign('wood', $wood);
			$smarty->display($templateDir . "/temp1/editWood.tpl");
		}
	}

	if($opt == "searchArticle" && $permit ==true)
	{
		$start = $p->findStart($configPage['limit']);
		$count = 0;
		$articleArr = NULL;
		if(isset($_GET['inputCode']) && $_GET['inputCode'] != '' && $_GET['inputCode'] !="Please input the code..")
		{
			$count = $articleService->countArticleByCode($context, $result, $_GET['inputCode']);
			$articleArr = $articleService->getArticleByCodeByRange($context, $result, $start, $configPage['limit'],$_GET['inputCode']);
		}
		if(isset($_GET['colorID']) && $_GET['colorID'] != 0)
		{
			$count = $articleService->countArticleByColorID($context, $result, $_GET['colorID']);
			$articleArr = $articleService->getArticleByColorIDByRange($context, $result, $start, $configPage['limit'],$_GET['colorID']);
		}
		if(isset($_GET['lineID']) && $_GET['lineID'] != 0)
		{
			$count = $articleService->countArticleByLineID($context, $result, $_GET['lineID']);
			$articleArr = $articleService->getArticleByLineIDByRange($context, $result, $start, $configPage['limit'],$_GET['lineID']);
		}
		if(isset($_GET['woodTypeID']) && $_GET['woodTypeID'] != 0)
		{
			$count = $articleService->countArticleByWood($context, $result, $_GET['woodTypeID']);
			$articleArr = $articleService->getArticleByWoodTypeIDByRange($context, $result, $start, $configPage['limit'],$_GET['woodTypeID']);
		}
			
		$pages = $p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$colourList = $colourService->findAllColour($context, $result);
		$lineList = $lineService->findAllLine($context, $result);
		$woodList = $woodService->findAllWood($context, $result);
		$smarty->assign('colourList', $colourList);
		$smarty->assign('lineList', $lineList);
		$smarty->assign("woodList",$woodList);
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);

		$smarty->assign('articleArr', $articleArr);
		$smarty->display($templateDir . "/temp1/listArticle.tpl");
	}
}
?>